package com.david.learning.algorithm;


/**
 * 计算N的二进制表示中的1的个数
 * @author David Qi
 *
 */
public class Count1NumberOfN {
	
	public static void main(String[] args) {
		System.out.println(count(17));
		System.out.println(count(18));
    }
	
	
	private static int count(int n) {
		if(n == 1) {
			return 1;
		}else if (n ==2){
			return 1;			
		}else {
			if(isEven(n)) {
				return count(n/2);
			}else {
				int i = (int)Math.floor(n/2d);
				return count(i)+1;
			}
		}
	}
			
	private static boolean isEven(double n) {
		return Double.valueOf(n/2d) == Math.ceil(n/2d)?true:false;
	}
}
